<?php

namespace Admin\Controller;
use Org\Util\Sync;

/**
 * 品类控制器
 * @author James
 * 2016-01-21
 */
class ClassifyController extends AdminController {

    /**
     * 品类列表
     * @author James
     * 2016-01-21
     */
    public function index() {
        $title = I('title');
        $map['status']  =   1;
        $map['title'] = array('like', '%' . (string) $title . '%');
        $list = $this->lists('classify', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '数据列表';
        $this->display();
    }

    /**
     * 新增品类
     * @author James
     * 2016-01-21
     */
    public function add($title = "") {
        if (IS_POST) {
            $data = array('title' => $title, 'createdAt' => NOW_TIME, 'updatedAt' => NOW_TIME, 'createdUser' => UID, 'updatedUser' => UID);
            $id = M('classify')->add($data);
            if (!$id) {
                $this->error("添加失败！");
            } else {
                $this->success('添加成功！', U('index'));
            }
        } else {
            $this->display();
        }
    }

    /**
     * 修改品类
     * @author James
     * 2016-01-21
     */
    public function edit($title = "") {
        $id = I('id');
        if (IS_POST) {
            $data = array('title' => $title, 'updatedAt' => NOW_TIME, 'updatedUser' => UID);
            if (!M('classify')->where(array('id' => $id))->save($data)) {
                $this->error("修改失败！");
            } else {
                $this->success('修改成功！', U('index'));
            }
        } else {
            $data = M('classify')->where(array('id' => $id))->find();
            $this->data = $data;
            $this->display();
        }
    }
    
    /**
     * 删除品类
     * @author James
     * 2016-01-21
     */
    public function del($id = 0) {
        if ($id) {
            $classify = M("classify");
            $data = array('status'=>-1,'updatedAt' => NOW_TIME, 'updatedUser' => UID);
            $result = $classify-> where(array('id'=>$id))->setField($data);
            
            if($result){
                $this->success('删除成功！', U('index'));
            }else{
                $this->error("删除失败！");
            }
        }
    }
    
    /**
     * 上推
     * @author wangyuanlin
     * 2015-06-23
     */
    public function push(){
        
        //获取要上推的数据源
        $list = M('Classify')->where(array('syncStatus'=>array('egt', 1)))->select();

        //执行上推操作
        $list = $this->pushParse('Classify',
            array('objectId'=>'id',
                'title'=>'title',
                'fileurl'=>'fileurl',
                'intro'=>'intro',
                'gjz'=>'gjz',
                'orderno'=>'ordnum',
                'isshow'=>'isshow',
                'status'=>'status',
                'syncStatus'=>'syncStatus'),
            function($src){
                //前调函数
                return $src;
            },
            function($src){
                //后调函数
                //数据上推到云端后，更新本地数据的同步状态 （syncStatus ＝ 0）
                M('Classify')->where('id='.$src['objectId'])->data(array('syncStatus'=>0))->save();
                return $src;
            },
            $list);
            
       $this->success('商品分类数据上推成功！', U('index'),true);
    }
}
